package com.grf.rocketmq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;

import lombok.SneakyThrows;

/**
 * @author : guorf
 * @version : v1.0
 * @description : TODO 生产者工具类
 * @createTime : 2023/9/13 11:16
 */
public class ProducerUtil {

    public static final String NAMESRV_ADDR = "localhost:9877";
    public static final String GROUP_NAME = "demo_group";

    public static final String TOPIC = "TopicTest";

    /**
     * @description TODO 创建生产者
     * @return org.apache.rocketmq.client.producer.DefaultMQProducer
     **/
    @SneakyThrows
    public static DefaultMQProducer start(){
        // 初始化一个producer并设置Producer group name
        DefaultMQProducer producer = new DefaultMQProducer(GROUP_NAME); //（1）
        // 设置NameServer地址
        producer.setNamesrvAddr(NAMESRV_ADDR);  //（2）
        // 启动producer
        producer.start();
        return producer;
    }

    public static void shutdown(DefaultMQProducer producer){
        if (producer != null) {
            producer.shutdown();
        }
    }
}
